//
//  YKActivityViewController.m
//  yuke
//
//  Created by Dejun Liu on 15/1/26.
//  Copyright (c) 2015年 juwan. All rights reserved.
//

#import "YKActivityViewController.h"
#import "YKMActivityCollectionViewCell.h"

@interface YKActivityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSArray *listData;

@end

@implementation YKActivityViewController

#pragma mark LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type == VCTypeClassicalActivity) {
        self.title = @"经典路线";
    }else
    {
        self.title = @"活动列表";
    }
    [self hideHomeNavigationBar];
    [self loadCollectionView];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(296, 199);
    layout.minimumInteritemSpacing = 15;
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(20, 25, 15, 25);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-49-64-70) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"YKMActivityCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    self.collectionView.scrollEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
}

- (void)requestData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:@{@"module":@"destinationlist"}];
    [params hs_setSafeValue:self.curPlace.cateid forKey:@"area"];
    [params hs_setSafeValue:self.curPlace.subcateid forKey:@"city"];
    if (self.type == VCTypeClassicalActivity) {
        [params hs_setSafeValue:@"classic" forKey:@"type"];
    }else
    {
        [params hs_setSafeValue:@"activity" forKey:@"type"];
    }
    [[JWWebService sharedInstance]getWithParams:params jsonResponse:^(id responseData, NSError *error) {
        if (responseData) {
            //城市
            {
                self.cityName.text = [[responseData objectForKey:@"list"] objectForKey:@"name"];
                self.totalLandmark.text = [NSString stringWithFormat:@"%@个景点",[[responseData objectForKey:@"list"] objectForKey:@"total"]];
            }
            
            //列表
            {
                self.listData = [YKLandmark listFromArrayDic:[[responseData objectForKey:@"list"] objectForKey:@"sublist"]];
                if (_listData.count) {
                    [self.collectionView reloadData];
                }
            }
        }
    }];
}

#pragma mark - Collection View -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.listData.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YKLandmark *lm = [self.listData objectAtIndex:indexPath.row];
    
    YKMActivityCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:lm.LMPic]];
    cell.mTitle.text = lm.LMName;
    cell.commentNum.text = [NSString stringWithFormat:@"%@条评论",lm.LMCommentNum];
    [cell.starView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton *button = obj;
        if (idx+1 <= [lm.LMStar intValue]) {
            [button setImage:[UIImage imageNamed:@"star1"] forState:0];
        }else
        {
            [button setImage:[UIImage imageNamed:@"star0"] forState:0];
        }
    }];
    cell.priceNow.text = lm.LMPriceNow;
    cell.pricePrev.text = lm.LMPricePre;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

@end
